import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:qlevar_router/qlevar_router.dart';
import 'package:progress_indicators/progress_indicators.dart';
import 'package:stacked/stacked.dart';
import '../../heartbeat/helpers/magma_constant.dart';
import '../../heartbeat/helpers/magma_router.dart';
import '../../comms_link/app_state.dart';

/// 启动页面视图模型
class SplashViewModel extends BaseViewModel {
  bool _hasNavigated = false;

  /// 初始化应用状态管理
  Future<void> initializeAppState() async {
    setBusy(true);
    try {
      // 初始化全局状态管理
      appState.initializeApp();
    } catch (e) {
      setError(e.toString());
    } finally {
      setBusy(false);
    }
  }

  /// 延迟跳转到登录页面
  Future<void> delayNavigateToLogin() async {
    if (_hasNavigated) return;

    _hasNavigated = true;
    await Future.delayed(const Duration(seconds: 3));

    if (QR.currentPath == MagmaRouter.splashPath) {
      QR.to(MagmaRouter.mainPath);
    }
  }
}

/// 启动页面
class SplashStory extends StatelessWidget {
  const SplashStory({super.key});

  @override
  Widget build(BuildContext context) {
    return ViewModelBuilder<SplashViewModel>.reactive(
      viewModelBuilder: () => SplashViewModel(),
      onViewModelReady: (viewModel) {
        // 初始化应用状态
        viewModel.initializeAppState();
        // 延迟跳转
        viewModel.delayNavigateToLogin();
      },
      builder: (context, viewModel, child) {
        return Scaffold(
          body: Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage(MagmaConstant.background),
                fit: BoxFit.cover,
              ),
            ),
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  // 应用 Logo
                  Container(
                    width: 90.w,
                    height: 90.w,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(12.r),
                      image: const DecorationImage(
                        image: AssetImage(MagmaConstant.logo),
                        fit: BoxFit.cover,
                      ),
                    ),
                  ),
                  SizedBox(height: 10.h),
                  // 应用名称 - 带动画效果
                  JumpingText(
                    MagmaConstant.appName,
                    style: TextStyle(
                      fontSize: 24.sp,
                      color: Colors.white,
                      fontFamily:
                          GoogleFonts.blackHanSans(
                            fontWeight: FontWeight.w400,
                            fontSize: 24.sp,
                            color: Colors.white,
                          ).fontFamily,
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
